package com.clb.studyproject.ui

import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.clb.studyproject.R

class AnimalActivity :AppCompatActivity(R.layout.activity_animal){

    private lateinit var framImg: ImageView
    private lateinit var tweenAnimation: ImageView
    private lateinit var objectAnim: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        framImg = findViewById(R.id.fram_image)
        tweenAnimation = findViewById(R.id.tween_animation)
        objectAnim = findViewById(R.id.object_animation)
    }

    fun frameAnimation(view: View) {
        val animationDrawable = framImg.background as AnimationDrawable
        animationDrawable.stop()
        animationDrawable.start()
    }

    fun tweenAnimation(view: View) {
        val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_set)
        tweenAnimation.startAnimation(loadAnimation)
    }

    fun objectAnim(view: View) {
        val ofFloat = ValueAnimator.ofFloat(0F, 400F)
        ofFloat.setTarget(objectAnim)
        ofFloat.setDuration(1000)
        ofFloat.repeatCount = -1
        ofFloat.repeatMode= ValueAnimator.REVERSE
        //设置更新监听
        //值 改变一次，该方法就执行一次
        //重放模式
        //ValueAnimator.START：正序
        //ValueAnimator.REVERSE：倒序

        //设置更新监听
        //值 改变一次，该方法就执行一次
        ofFloat.addUpdateListener(AnimatorUpdateListener { animation ->
            val value = animation.animatedValue as Float //value应该是根据设置的offFloat值变化的
            objectAnim.setTranslationX(value)
            objectAnim.setRotation(value)
        })
        ofFloat.start()

    }
}
